package com.athenaeum.dataxplore.controller;

import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.athenaeum.datastore.domain.Experience;
import com.athenaeum.datastore.domain.User;
import com.athenaeum.datastore.service.OrganizationService;
import com.athenaeum.datastore.service.UserService;
import com.athenaeum.datastore.util.UserUtil;

@Controller
public class SettingsController {

	private static final Logger log = LoggerFactory
			.getLogger(SettingsController.class);

	@Autowired
	private UserService userService;

	@Autowired
	private OrganizationService organizationService;

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/settings/profile", method = RequestMethod.GET)
	public String viewProfile(Locale locale, Model model) {
		log.info("Loading Settings - Profile Page...");
		model.addAttribute("profile", true);
		User user = userService.findOne(UserUtil.getUsernameInContext());
		model.addAttribute("user", user);
		model.addAttribute("experience", new Experience());
		model.addAttribute("experiences", user.getExperiences().values());
		return "settings/profile";
	}

	@RequestMapping(value = "/settings/profile/save", method = RequestMethod.POST)
	public String saveProfile(Locale locale, @ModelAttribute("user") User user,
			BindingResult result, Model model) {
		log.info("Saving Settings - Profile Page...");
		if (result.hasErrors())
			return "settings/profile";

		user = userService.save(user);
		return "redirect:/settings/profile";
	}

	@RequestMapping(value = "/settings/account", method = RequestMethod.GET)
	public String account(Locale locale, Model model) {
		log.info("Loading Settings - Account Page...");
		model.addAttribute("account", true);
		User user = userService.findOne(UserUtil.getUsernameInContext());
		model.addAttribute("user", user);
		return "settings/account";
	}

	@RequestMapping(value = "/settings/notifications", method = RequestMethod.GET)
	public String viewNotifications(Locale locale, Model model) {
		log.info("Loading Settings - Notifications Page...");
		model.addAttribute("notifications", true);
		return "settings/notifications";
	}

	@RequestMapping(value = "/settings/applications", method = RequestMethod.GET)
	public String viewApplications(Locale locale, Model model) {
		log.info("Loading Settings - Applications Page...");
		model.addAttribute("applications", true);
		return "settings/applications";
	}

	@RequestMapping(value = "/settings/organizations", method = RequestMethod.GET)
	public String viewOrganizations(Locale locale, Model model) {
		log.info("Loading Settings - Organizations Page...");
		model.addAttribute("organizations", true);

		model.addAttribute("userorganizations", organizationService.findAll());
		return "settings/organizations";
	}
}
